package com.lzh.controller;

import com.lzh.pojo.Account;
import com.lzh.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {
    /*
    * spring和springmvc容器是有层次的（父子）
    * spring中：service和dao
    * */
    @Autowired
    private AccountService accountService;


    @RequestMapping("/queryAll")
    public String queryAll(Model model){
        List<Account> list = accountService.selectAllAccount();
        model.addAttribute("list",list);
        return "queryAll";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "addAccount";
    }

    @RequestMapping("addAccount")
    public String addAccount(Account account){
        accountService.addAcount(account);
        return "redirect:/account/queryAll";
    }

    //去更新
    @RequestMapping("/toUpdateAccount")
    public String toUpdateBook(int id,Model model) {
        Account account = accountService.selectById(id);
        model.addAttribute("account",account);
        return "updateAccount";
    }
    //更新
    @RequestMapping("/updateAccount")
    public String updateAccount(Account account){
        System.out.println("updateAccount=>"+account);
        accountService.updateAccount(account);
        return "redirect:/account/queryAll";
    }

    @RequestMapping("/deleteAccount/{id}")
    public String DeleteAccount(@PathVariable("id") int id){
        accountService.deleteById(id);
        return "redirect:/account/queryAll";
    }
}
